package com.example.w_library.log.priter.view;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.w_library.R;
import com.example.w_library.log.base.WLogConfig;
import com.example.w_library.log.base.WLogType;
import com.example.w_library.log.priter.WLogPrinter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 可视化打印器
 */
public class WLogViewPrinter implements WLogPrinter {

    private RecyclerView recyclerView;
    private LogAdapter adapter;
    //可视化显示辅助类
    private WViewPrinterProvider printerProvider;
    /**
     * 构造方法实例化 RecyclerView
     * @param activity
     */
    public WLogViewPrinter(Activity activity){
        FrameLayout rootView = activity.findViewById(android.R.id.content);
        recyclerView = new RecyclerView(activity);
        adapter = new LogAdapter(LayoutInflater.from(recyclerView.getContext()));
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        printerProvider = new WViewPrinterProvider(rootView,recyclerView);
    }

    /**
     * 获取显示辅助器
     * @return
     */
    public WViewPrinterProvider getPrinterProvider() {
        return printerProvider;
    }

    @Override
    public void print(@NotNull WLogConfig config, int level, String tag, @NotNull String printString) {
        //添加数据到recycleView
        adapter.addItem(new WLogBean(System.currentTimeMillis(),level,tag,printString));
        //滚动到对应位置
        recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);
    }

    /**
     * recyclerView 适配器
     */
    private static class LogAdapter extends RecyclerView.Adapter<LogViewHolder> {

        private LayoutInflater layoutInflater;
        private List<WLogBean> logs = new ArrayList<>();

        /**
         * 构造方法
         *
         * @param layoutInflater
         */
        public LogAdapter(LayoutInflater layoutInflater) {
            this.layoutInflater = layoutInflater;
        }

        /**
         * 添加单条数据
         *
         * @param logBean
         */
        public void addItem(WLogBean logBean) {
            logs.add(logBean);
            notifyItemInserted(logs.size() - 1);
        }

        @NonNull
        @Override
        public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = layoutInflater.inflate(R.layout.log_view, parent, false);
            return new LogViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull LogViewHolder holder, int position) {
            WLogBean logBean = logs.get(position);
            int color = getHighlightColor(logBean.level);
            holder.tvTag.setTextColor(color);
            holder.tvMsg.setTextColor(color);
            holder.tvTag.setText(logBean.getFlattened());
            holder.tvMsg.setText(logBean.log);
        }

        @Override
        public int getItemCount() {
            return logs.size();
        }

        /**
         * 获取日志颜色
         *
         * @param logLevel 级别
         * @return color
         */
        private int getHighlightColor(int logLevel) {
            int highlight;
            switch (logLevel) {
                case WLogType.V:
                    highlight = 0xffbbbbbb;
                    break;
                case WLogType.D:
                    highlight = 0xffffffff;
                    break;
                case WLogType.I:
                    highlight = 0xff6a8759;
                    break;
                case WLogType.W:
                    highlight = 0xffbbb529;
                    break;
                case WLogType.E:
                    highlight = 0xffff6b68;
                    break;
                default:
                    highlight = 0xffffff00;
                    break;
            }
            return highlight;
        }

    }

    /**
     * viewHolder
     */
    private static class LogViewHolder extends RecyclerView.ViewHolder {

        public TextView tvTag;
        public TextView tvMsg;

        public LogViewHolder(@NonNull View itemView) {
            super(itemView);

            tvTag = itemView.findViewById(R.id.tv_tag);
            tvMsg = itemView.findViewById(R.id.tv_msg);
        }
    }


}
